import { Session } from '@/utils/storage';
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';

// 配置request访问实例
export const service: AxiosInstance = axios.create({
    baseURL: '',
    timeout: 5000,
    headers: { 'Content-Type': 'application/json' }
})

service.interceptors.request.use(
    // 发送请求前检查有无token，有的话，放在请求头中
    (config: AxiosRequestConfig) => {
        if (Session.get("token")) {
            config.headers!["Authorization"] == `${Session.get('token')}`
        }
        return config;
    },
    // 错误逻辑处理
    (error) => {
        Promise.reject(error);
    }
);


service.interceptors.response.use(
    (response) => {
        const res = response.data;
        return res;
    },
    (error) => {
        Promise.reject(error);
    }
)

export default service;